package com.ruoyi.web.core.config;

import io.netty.channel.nio.NioEventLoopGroup;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.Codec;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;

@Configuration
public class RedissonConfig {

    @Value("${spring.redisson.address}")
    private String address;

    @Value("${spring.redisson.database}")
    Integer database;

    @Value("${spring.redisson.password}")
    private  String password;

    @Value("${spring.redisson.retryAttempts}")
    private Integer retryAttempts;

    @Value("${spring.redisson.codec}")
    private  String codec;

    @Value("${spring.redisson.thread}")
    private Integer thread;


    @Bean(destroyMethod = "shutdown")
    RedissonClient redisson() throws Exception {
        Config config = new Config();
        config.useSingleServer().setAddress(address)
                .setDatabase(database)
                .setPassword(password)
                .setRetryAttempts(retryAttempts);
        Codec temp=(Codec)ClassUtils.forName(codec,ClassUtils.getDefaultClassLoader()).newInstance();
        config.setCodec(temp);
        config.setThreads(thread);
        config.setEventLoopGroup(new NioEventLoopGroup());
        config.setLockWatchdogTimeout(30000L);
        return Redisson.create(config);
    }
}
